# Maintainer: David Grayson <davidegrayson@gmail.com>

_realname=avrdude
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=7.0
pkgrel=1
pkgdesc='Software for programming Atmel AVR Microcontrollers (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.nongnu.org/avrdude/"
license=('GPL')
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-hidapi"
         "${MINGW_PACKAGE_PREFIX}-libelf"
         "${MINGW_PACKAGE_PREFIX}-libftdi"
         "${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-libusb-compat"
         "${MINGW_PACKAGE_PREFIX}-libusb-win32"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
options=('staticlibs' 'strip')
source=("https://download.savannah.gnu.org/releases/avrdude/avrdude-${pkgver}.tar.gz"{,.sig}
        '01-libtool.patch'
        '02-ddk-headers.patch'
        '03-winsock.patch')
sha256sums=('c0ef65d98d6040ca0b4f2b700d51463c2a1f94665441f39d15d97442dbb79b54'
            'SKIP'
            '446739442932386b4d7dd076599c81188e78332fe7e2b8d0d0459b767ec5e1aa'
            '4ea0bbff0c0f101dd560d183fd7792ddbef0e9cd71c7c455adcb8af577e954de'
            '7246ac25f5c72e14379747a4efb3a1d4f9b8b10419537c08c5bbf686ddf1a61c')
validpgpkeys=('EF497ABE47ED91B3FC3D7EA54D902FF7723BDEE9'
              '5E84F980C3CAFD4BB5841070F48CA81B69A85873'
              '56628323218C669FF578705C7E9EADC3030D34EB')

prepare()
{
  cd "${srcdir}/${_realname}-${pkgver}"

  # Fixes a warning from libtool.  However, a DLL still does not generated.
  patch -p1 -i ../01-libtool.patch

  # Look for headers like hidsdi.h and hidpi.h at the top level,
  # (do not assume they are inside the "ddk" directory).
  patch -p1 -i ../02-ddk-headers.patch

  # Removal of this AC_CHECK_LIB causes linker errors
  patch -p1 -i ../03-winsock.patch

  ./bootstrap
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  CFLAGS+=" -I${MINGW_PREFIX}/include/libusb-compat"
  "../${_realname}-${pkgver}/configure" \
    --prefix="${MINGW_PREFIX}" \
    --build="${MINGW_CHOST}" \
    --host="${MINGW_CHOST}"

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  make check
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install

  # As we can see from confwin.c, AVRDUDE searches for avrdude.conf
  # using the PATH; it does not find it in /etc.
  mv "${pkgdir}"${MINGW_PREFIX}/etc/avrdude.conf "${pkgdir}"${MINGW_PREFIX}/bin
}
